<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goods">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.categories.id }}</XtxBreadItem>
        <XtxBreadItem to="/">{{}}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.name }}</XtxBreadItem>
      </XtxBread>

      <!-- 商品信息 -->
      <div class="goods-info">
        <!--左边-->
        <div class="media">
          <!-- 区域1 商品图片组件 -->
          <XtxImageView :imageList="goods.mainPictures" />
          <!-- 区域2 商品销售信息组件 -->
          <GoodsSales :goodsData="goods" />
        </div>
        <!--右边-->
        <div class="spec">
          <!-- 区域3 商品介绍组件 -->
          <GoodsName :goodsData="goods" />
          <!-- 区域4 商品规格组件  -->
          <XtxGoodSku :goods="goods" @change="skuChange" />
          <!-- 区域5 数量选择组件
            （1）显示初始值
             (2) 当前商品的库存
          -->
          <!-- <XtxNumbox v-model="num" :max="curSku.inventory"/> -->
          <XtxNumbox v-model="count" :max="curSku.inventory" />
          <!-- 加入购物车 按钮 -->
          <!-- <XtxButton type="primary" style="margin-top: 20px">加入购物车</XtxButton> -->
          <XtxButton type="primary" style="margin-top: 20px" @click="insertCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <div class="goods-relevant">商品推荐</div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->

          <GoodsDetail :goods="goods" />
          <!-- 注意事项 -->
          <div class="goods-warn">注意事项</div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <!-- 24小时热榜 -->
          <GoodsHot hotType="1" />
          <!-- 一周热榜 -->
          <GoodsHot hotType="2" />
          <!-- 总热销榜 -->
          <GoodsHot hotType="3" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { findGoods } from '@/api/goods'
import { useRoute, useRouter } from 'vue-router'
import GoodsName from './Components/GoodsName'
import GoodsSales from './Components/GoodsSales.vue'
import GoodsDetail from './Components/GoodsDetail.vue'
import GoodsHot from './Components/GoodsHot.vue'
import { useStore } from 'vuex'
import { Message } from '@/components/Message/index.js'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsName, GoodsSales, GoodsDetail, GoodsHot },
  setup() {
    const store = useStore()
    // 商品详情初始值为null
    const goods = ref(null) // goods.skus ===> error!!

    // 用户选择的SKU
    const curSku = ref({ inventory: 1, oldPrice: 0, skuId: '', specsText: '' })
    const route = useRoute()
    const router = useRouter()
    // findGoods(当前的商品的id)
    findGoods(route.params.id).then(res => {
      goods.value = res.result
    })

    const skuChange = sku => {
      console.log(sku)
      curSku.value = sku
    }
    // const num = ref(2) // 用户选择的商品的数量,初始值为101
    const count = ref(1) // 用户选择的商品的数量,初始值为1
    // return { goods, skuChange, num, curSku }
    const insertCart = async () => {
      // 判断是否登录
      if (!store.state.user.profile.token) {
        // 回到login，并携带本页面的地址
        //  为了从login登录之后，能再回来
        const path = encodeURIComponent(route.fullPath)
        router.push('/login?backto=' + path)
      }

      // 调用接口，发请求
      // 商品的SKUID, 数量
      const skuId = curSku.value.skuId
      // 是否选择了有效商品属性
      if (!skuId) {
        Message({ type: 'warn', text: '请先选择商品属性' })
        return
      }

      await store.dispatch('cart/asyncSetCartList', { skuId, count: count.value })
      Message({ type: 'success', text: '添加成功' })
    }

    return { goods, skuChange, count, curSku, insertCart }
  }
}
</script>
<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}

.number-box {
  display: flex;
  align-items: center;
  .label {
    width: 60px;
    color: #999;
    padding-left: 10px;
  }
}
.g-name {
  font-size: 22px;
}
.g-desc {
  color: #999;
  margin-top: 10px;
}
.g-price {
  margin-top: 10px;
  span {
    &::before {
      content: '¥';
      font-size: 14px;
    }
    &:first-child {
      color: @priceColor;
      margin-right: 10px;
      font-size: 22px;
    }
    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}
.g-service {
  background: #f5f5f5;
  width: 500px;
  padding: 20px 10px 0 10px;
  margin-top: 10px;
  dl {
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    dt {
      width: 50px;
      color: #999;
    }
    dd {
      color: #666;
      &:last-child {
        span {
          margin-right: 10px;
          &::before {
            content: '•';
            color: @xtxColor;
            margin-right: 2px;
          }
        }
        a {
          color: @xtxColor;
        }
      }
    }
  }
}
.goods-sales {
  display: flex;
  width: 400px;
  align-items: center;
  text-align: center;
  height: 140px;
  li {
    flex: 1;
    position: relative;
    ~ li::after {
      position: absolute;
      top: 10px;
      left: 0;
      height: 60px;
      border-left: 1px solid #e4e4e4;
      content: '';
    }
    p {
      &:first-child {
        color: #999;
      }
      &:nth-child(2) {
        color: @priceColor;
        margin-top: 10px;
      }
      &:last-child {
        color: #666;
        margin-top: 10px;
        i {
          color: @xtxColor;
          font-size: 14px;
          margin-right: 2px;
        }
        &:hover {
          color: @xtxColor;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
